﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Model
{
    /// <summary>
    /// 通用返回信息类
    /// </summary>
    public class ResponseModel
    {
        /// <summary>
        /// 请求响应实体类
        /// </summary>
        public ResponseModel()
        {
        }
        /// <summary>
        /// 响应代码
        /// </summary>
        public int Code { get; set; } = 200;
        /// <summary>
        /// 响应消息内容
        /// </summary>
        public string Message { get; set; } = "操作成功";
        /// <summary>
        /// 返回的响应数据
        /// </summary>
        public object Data { get; set; }

        /// <summary>
        /// 时间
        /// </summary>
        public string TimeSpan { get { return Timestamp(); } }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string Timestamp()
        {
            long ts = ConvertDateTimeToInt(DateTime.Now);
            return ts.ToString();
        }

        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        public long ConvertDateTimeToInt(System.DateTime time)
        {
            long t = (time.Ticks - 621356256000000000) / 10000;
            return t;
        }

        /// <summary>
        /// 设置响应状态为成功
        /// </summary>
        /// <param name="message"></param>
        public void SetSuccess(string message = "成功")
        {
            Code = 200;
            Message = message;
        }
        /// <summary>
        /// 设置响应状态为失败
        /// </summary>
        /// <param name="message"></param>
        public void SetFailed(string message = "失败")
        {
            Message = message;
            Code = 407;
        }

        /// <summary>
        /// 设置响应状态参数错误
        /// </summary>
        /// <param name="message"></param>
        public void SetParamError(string message = "参数错误")
        {
            Message = message;
            Code = 101;
        }

        /// <summary>
        /// 设置响应状态为体验版(返回失败结果)
        /// </summary>
        /// <param name="message"></param>
        public void SetIsTrial(string message = "体验版,功能已被关闭")
        {
            Message = message;
            Code = 999;
        }

        /// <summary>
        /// 设置响应状态为错误
        /// </summary>
        /// <param name="message"></param>
        public void SetError(string message = "错误")
        {
            Code = 500;
            Message = message;
        }

        /// <summary>
        /// 设置响应状态为未知资源
        /// </summary>
        /// <param name="message"></param>
        public void SetNotFound(string message = "未知资源")
        {
            Message = message;
            Code = 404;
        }

        /// <summary>
        /// 设置响应状态为无权限
        /// </summary>
        /// <param name="message"></param>
        public void SetNoPermission(string message = "无权限")
        {
            Message = message;
            Code = 401;
        }
        /// <summary>
        /// 设置响应状态为无用户
        /// </summary>
        /// <param name="message"></param>
        public void SetNoUser(string message = "获取用户失败")
        {
            Message = message;
            Code = 491;
        }
        /// <summary>
        /// 设置响应数据
        /// </summary>
        /// <param name="data"></param>
        public void SetData(object data)
        {
            Data = data;
        }
    }
}
